<template>
  <div class="myContainer">
    <!-- 头部标题 start -->  
    <el-row class="myHeadeTitle">
      <el-col :span="24" class="myPadding_20px">
        <img src="static/images/wbStep2.png" alt="" title=""/>
      </el-col>
    </el-row>
    <!-- 头部标题 end -->
    <!-- <el-row>
      <el-col>
        <el-form :inline="true" >
          <el-form-item>
            <el-input v-model="searchKW" class="myDialogSearchText" placeholder="输入关键字进行搜索"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="myBgGreen">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row> -->
    <el-row class="myPaddingTop_10px">
      <el-col :span="24">
        <template>
          <el-table
            ref="multipleTable"
            v-loading="loading"
            :data="subjectList"
            border
            @selection-change="handleSelectionChange"
            style="width: 100%">
            <el-table-column 
              type="selection"
              width="100">
            </el-table-column>
            <!-- <el-table-column
              width="100">
              <template scope="scope">
                <el-checkbox @change="selectIndex(scope.$index)" label=""></el-checkbox>
              </template>
            </el-table-column> -->
            <el-table-column
              type="index"
              label="序号"
              width="100">
            </el-table-column>
            <el-table-column
              prop="subject_content"
              label="执行项目名称">
            </el-table-column>
          </el-table>
        </template>
      </el-col>
    </el-row>
    <el-row class="myPaddingTop_10px">
      <el-col :span="24">
        <el-button type="primary" class="myBgGreen" @click="onPrevStep">上一步</el-button>
        <el-button type="primary" class="myBgGreen" @click="onNextStep">下一步</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myHeadeTitle{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    padding-bottom: 10px;
    border-bottom: 1px solid #dbdbdb;
    margin-bottom:10px; 
  }
  .myHeadeTitle:before,.myHeadeTitle:after{
    content: " ";
    display: block;
    clear: both;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top: 2px;
  }
  .myLineHeight_35px{
    line-height: 35px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }  
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
    padding-right: 10px;
  }
  .myPaddingBottom_10px{
    padding-bottom: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .myPadding_20px{
    padding: 20px;
  }
  .myDialogSearchText{
    width: 360px;
  }
  .myLineHeight_45px{
    line-height: 45px;    
  }

</style>

<script>

  import { getExecutionItemList } from '@/api/maintenanceplan';
  
  export default {
    data(){
      return{
        tableDataList:[],
        searchKW:"",
        id:"",
        subjectList:[],
        check:true,
        index:[],
        defaultIndex:[],
        loading:false,
        planname:""
      }
    },
    components: {
        
    },
    mounted(){
      document.title = '模板任务计划管理';
      this.id = this.$route.query.id;
      this.planname = this.$route.query.planname;
      this.ongetExecutionItemList();
    },
    methods:{     
      handleSelectionChange:function(val){
        let vm=this;
        vm.index=[];
        vm.defaultIndex=[];
        for(let i=0;i<val.length;i++){//获取选中列的下标
          vm.index.push(val[i].idx);
        }
        for(let i=0;i<vm.subjectList.length;i++){//获取拉取数据数组中的全部下标
          vm.defaultIndex.push(i);
        }
        vm.array_diff(vm.defaultIndex,vm.index);
        console.log("需要提交的执行项下标：",vm.defaultIndex)
      },
      onPrevStep:function(){
        // this.$router.go(-1);
        this.$router.push({
          path:"/wbjhglxx",
          query:{
            type:"back"
          }
        })
      },
      onNextStep:function(){
        let vm=this;
        let firstStepData=window.localStorage.getItem("firstStepData");
        if(firstStepData!=""){
          let firstStepDataJSON=JSON.parse(firstStepData);
          firstStepDataJSON.invalid_subjects=vm.defaultIndex;
          window.localStorage.setItem("secondStepData",JSON.stringify(firstStepDataJSON));//第一步数据+不执行项的下标
          console.log("制定维保计划第二步数据",firstStepDataJSON);
          vm.$router.push({
            path:'/wbjhglrw',
            query: {
              type: 'add', 
              id: vm.id , 
              cycle: firstStepDataJSON.plan_peroid , 
              planname:vm.planname , 
              target_objectid:firstStepDataJSON.target_objectid
            }
          })
        }else{
          console.log("跳转失败，没有第一步的缓存数据");
        }
        
      },
      ongetExecutionItemList(){//通过模板id拉取执行项列表
        let vm=this;
        vm.loading=true;
        getExecutionItemList(vm.id).then(response => {
          let res=response.data;
          vm.subjectList=res.rows;
          for (let i = 0; i < res.rows.length; i++) {
            vm.subjectList[i] = res.rows[i];
            vm.subjectList[i].idx = i;
          }
          console.log("拉取执行项成功",res.rows);
          console.log(this.subjectList);
          vm.loading=false;
          // setTimeout(function(){
            
          //   res.rows.forEach(row => {
          //     console.log("++++",row)
          //     vm.$refs.multipleTable.toggleRowSelection(row);
          //   });

          // },150)

        }).catch(error => {
          //拉取失败
          console.log("拉取执行项失败",error);
          vm.loading=false;
        })
      },
      array_diff(a, b) {//去除数组重复项  
        for(var i=0;i<b.length;i++){  
          for(var j=0;j<a.length;j++){  
            if(a[j]==b[i]){  
              a.splice(j,1);  
              j=j-1;  
            }  
          }  
        }   
        return a;  
      }
    }
  
  }


</script>



